package com.zf.shuashua.ui.mine;

import android.content.Intent;

import com.zf.shuashua.utils.ToastUtil;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.Me;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class MineFragmVM extends ViewModel {

    private MutableLiveData<Me> me;

    public MineFragmVM() {
    }

    public MutableLiveData<Me> getMe() {
        if (me == null) {
            me = new MutableLiveData<>();
        }
        return me;
    }

    public void getData() {
        HttpData.getInstance().getMe(new ACallback<Me>() {

            public void onRealSuccess(Me data) {
                if (data.isStatus()) {
                    getMe().setValue(data);
                    if (data.getData() != null) {
                        Store.getInstance().setNEW_OR_OLD(data.getData().isNew_or_old());//新老用户
                        ShuaApplication.game_status = data.getData().isGame_status();
                        ShuaApplication.is_bind_wechat = data.getData().isIs_bind_wechat();
                    }
                }
            }

            @Override
            public void onSuccess(Me data) {
                if (data.getCode() == 402 || data.getCode() == 406) {
                    ToastUtil.showToast(ShuaApplication.getContext(), "请重新登录");
                    Store.getInstance().logoutSiglent(ShuaApplication.getContext());
                    Intent intent = new Intent(ShuaApplication.getContext(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ShuaApplication.getContext().startActivity(intent);
                } else if (data.getCode() == 401) {
                    //ToastUtil.showToast(ShuaApplication.getContext(),"登录过期");
                    Store.getInstance().logoutSiglent(ShuaApplication.getContext());
                } else if (data.getData() != null) {
                    onRealSuccess(data);
                } else {
//                    ToastUtil.showToast(ShuaApplication.getContext(), "error code is  " + data.getCode());
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                LogUtil.d(errMsg);
            }
        });


    }


}